package com.hunlian.makelove.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.NumberPicker.Formatter;
import android.widget.NumberPicker.OnScrollListener;
import android.widget.NumberPicker.OnValueChangeListener;

import com.hunlian.makelove.R;


/**
 * 年、月、日 选择器
 * @author zealjiang
 * @time 2015年11月8日下午2:34:50
 */
public class HeightSelectorDialog extends Dialog implements OnValueChangeListener,OnScrollListener,Formatter{

	private Context mContext;
	private HeightSelectorDialog dialog;
	private NumberPicker mNpHeight;
	private Button mBtnCancel,mBtnSure;
	private int mLastHeight = 165;
	private Handler mHandler;
	public static final int VALUE = 10000;

	public HeightSelectorDialog(Context context) {
		super(context);
		mContext = context;
		dialog = this;
	}

	public HeightSelectorDialog(Context context, int theme,
			Handler handler) {
		super(context, theme);
		mContext = context;
		dialog = this;
		mHandler = handler;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.dialog_height_selector);
		initView();
		initData();
		setListener();

		//在使用的过程中，用户点击数字会弹出键盘，有时候会造成布局被挤压不好看，也有其他的需求
		mNpHeight.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

	}

	private void initView() {
		mNpHeight = (NumberPicker) this.findViewById(R.id.np_height);
		mBtnCancel = (Button) this.findViewById(R.id.btn_cancel);
		mBtnSure = (Button) this.findViewById(R.id.btn_sure);
	}

	private void initData() {
		int defaultValue = 168;
		mNpHeight.setFormatter(this);
		mNpHeight.setOnValueChangedListener(this);
		mNpHeight.setOnScrollListener(this);
		mNpHeight.setMaxValue(230);
		mNpHeight.setMinValue(100);
		mNpHeight.setValue(defaultValue);

		//mLastHeight = defaultValue;
	}
	
	private void setListener(){
		mBtnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}

		});
		
		mBtnSure.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int num = mNpHeight.getValue();
				mLastHeight = num;
				//Toast.makeText(mContext, num+value, 0).show();
				Message msg = new Message();
				Bundle b = new Bundle();
				b.putString("height", num+" cm");
				msg.setData(b);
				msg.what = VALUE;
				mHandler.sendMessage(msg);
				dialog.dismiss();
			}

		});
	}

	@Override
	public String format(int value) {
/*		String tmpStr = String.valueOf(value);
	       if (value < 10) {
	           tmpStr = "0" + tmpStr;
	       }
	       return tmpStr;*/
		return value+"";
	}

	@Override
	public void onScrollStateChange(NumberPicker view, int scrollState) {

	}

	@Override
	public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

		if(picker==mNpHeight){
			mLastHeight = newVal;
		}
		

	}
}
